import axios from 'axios';
import { ElMessage } from 'element-plus';
import qs from 'qs';
/**
 * GET公共请求
 * @param {*} url 请求地址
 * @param {*} params 请求参数
 */
function getAxios (url, params) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'GET',
      url: url,
      params: params
    })
      .then(res => {
        if (res.status !== 200) {
          reject(res, reject);
        } else {
          if (res.data.status === 0) {
            ElMessage.error(res.data.msg);
            reject(res, reject);
          } else {
            resolve(res.data, resolve);
          }
        }
      })
      .catch(error => {
        console.log(error)
        reject(error, reject);
      });
  });
}

/**
 * POST PUT 请求的公共方法
 * @param {*} url 请求地址
 * @param {*} params 请求参数
 * @param {*} type 请求类型
 */
function commonAxios (url, params, type) {
  return new Promise((resolve, reject) => {
    axios({
      method: type,
      url: url,
      data: params,
      transformRequest: [
        function (data) {
          let ret = '';
          for (let it in data) {
            ret +=
              encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
          }
          return ret;
        }
      ]
    })
      .then(res => {
        // resolve(res.data);
        if (res.status !== 200) {
          reject(res, reject);
        } else {
          if (res.data.status === 0) {
            ElMessage.error(res.data.msg);
            reject(res, reject);
          } else {
            resolve(res.data, resolve);
          }
        }
      })
      .catch(error => {
        console.log(error);
        reject(error);
      });
  });
}

/**
 * POST PUT 请求的公共方法json 方式传递
 * @param {*} url
 * @param {*} params
 * @param {*} type
 */
function commonAxiosJson (url, params, type) {
  // if (type === 'put' || type === 'PUT') {
  //   url = url + '/' + params.id
  // }
  return new Promise((resolve, reject) => {
    axios({
      method: type,
      url: url,
      data: params
    })
      .then(res => {
        if (res.status !== 200) {
          reject(res, reject);
        } else {
          if (res.data.status === 0) {
            ElMessage.error(res.data.msg);
            reject(res, reject);
          } else {
            resolve(res.data, resolve);
          }
        }
      })
      .catch(error => {
        reject(error);
      });
  });
}

/**
 * POST PUT 请求的公共方法json 方式传递
 * @param {*} url
 * @param {*} params
 * @param {*} type
 */
function updateBatchAxiosJson (url, params, type) {
  return new Promise((resolve, reject) => {
    axios({
      method: type,
      url: url,
      data: params
    })
      .then(res => {
        if (res.status !== 200) {
          reject(res, reject);
        } else {
          if (res.data.status === 0) {
            ElMessage.error(res.data.msg);
            reject(res, reject);
          } else {
            resolve(res.data, resolve);
          }
        }
      })
      .catch(error => {
        reject(error.data);
      });
  });
}

/**
 * 复杂对象使用该方法后端使用
 * @param {*} url
 * @param {*} params
 * @param {*} type
 */
function commonAxiosStringify (url, params, type) {
  return new Promise((resolve, reject) => {
    axios({
      method: type,
      url: url,
      data: params,
      paramsSerializer: function (params) {
        /* arrayFormat: 'repeat'：params是数组类型如arr=[1,2]，则转换成arr=1&arr=2 */
        return qs.stringify(params, {
          arrayFormat: 'repeat'
        });
      }
    })
      .then(res => {
        if (res.status !== 200) {
          reject(res, reject);
        } else {
          if (res.data.status === 0) {
            ElMessage.error(res.data.msg);
            reject(res, reject);
          } else {
            resolve(res.data, resolve);
          }
        }
      })
      .catch(error => {
        reject(error.data);
      });
  });
}

/**
 * DELETE公共请求
 * @param {*} url 请求地址
 * @param {*} params 请求参数
 */
function deleteAxios (url, params) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'DELETE',
      url: url,
      params: params
      // paramsSerializer: function (params) {
      //   /* params是数组类型如arr=[1,2]，则转换成arr=1&arr=2 */
      //   return qs.stringify(params, {
      //     arrayFormat: 'repeat'
      //   });
      // }
    })
      .then(res => {
        if (res.status !== 200) {
          reject(res, reject);
        } else {
          if (res.data.status === 0) {
            ElMessage.error(res.data.msg);
            reject(res, reject);
          } else {
            resolve(res.data, resolve);
          }
        }
      })
      .catch(error => {
        reject(error, reject);
      });
  });
}

/**
 * 先只传一个值的
 * @param {*} url 
 * @param {*} params 
 */
function deleteAxiosById (url, params) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'DELETE',
      url: url + '/' + params.idArray[0]
    })
      .then(res => {
        if (res.status !== 200) {
          reject(res, reject);
        } else {
          if (res.data.status === 0) {
            ElMessage.error(res.data.msg);
            reject(res, reject);
          } else {
            resolve(res.data, resolve);
          }
        }
      })
      .catch(error => {
        reject(error, reject);
      });
  });
}

/**
 * 图片上传请求
 * @param {*} url 请求地址
 * @param {*} params 请求参数
 */
function uploadImg (url, params) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'POST',
      url: url,
      data: params,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
      .then(res => {
        if (res.status !== 200) {
          reject(res, reject);
        } else {
          if (res.data.status === 0) {
            ElMessage.error(res.data.msg);
            reject(res, reject);
          } else {
            resolve(res.data, resolve);
          }
        }
      })
      .catch(error => {
        reject(error, reject);
      });
  });
}

/* 导出excel 请求 */
function exportExcelAxios (url, type, params) {
  return new Promise((resolve, reject) => {
    axios({
      method: type,
      url: url,
      data: params,
      /* 这里返回类型设置是关键 */
      responseType: 'blob',
      transformRequest: [
        function (data) {
          let ret = '';
          for (let it in data) {
            ret +=
              encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
          }
          return ret;
        }
      ]
    }).then(res => {
      if (res.status !== 200) {
        reject(res, reject);
      } else {
        if (res.data.status === 0) {
          ElMessage.error(res.data.msg);
          reject(res, reject);
        } else {
          resolve(res.data, resolve);
        }
      }
    }).catch(error => {
      reject(error, reject);
    });
  });
}

export {
  getAxios,
  commonAxios,
  commonAxiosJson,
  commonAxiosStringify,
  updateBatchAxiosJson,
  deleteAxios,
  deleteAxiosById,
  uploadImg,
  exportExcelAxios
};